<?php
/**
 * Created by PhpStorm.
 * User: CY
 * Date: 2018/3/23
 * Time: 15:27
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\OrderProduct;
use app\api\validate\IDMustBePositiveInt;
use app\api\validate\OrderPlace;
use app\api\service\Token;
use app\api\model\Order as OrderModel;
use app\api\service\Order as OrderService;
use app\api\validate\PagingParameter;
use app\lib\exception\OrderException;
use app\lib\exception\SuccessMessage;

class Order extends BaseController
{
    /**
     * 前置方法，进行权限验证
     * @var array
     */
    protected $beforeActionList = [
        'checkExclusiveScope' => ['only' => 'placeOrder'],
        'checkPrimaryScope' => ['only' => 'getDetail,getSummaryByUser'],
        'checkSuperScope' => ['only' => 'delivery,getSummary']
    ];

    /**
     * 用户下单
     * @url /order
     * @return array
     */
    public function placeOrder(){
        (new OrderPlace())->goCheck();
        $products = input('post.products/a');
        $uid = Token::getCurrentUid();
        $order = new OrderService();
        $status = $order->place($uid,$products);
        return $status;
    }

    /**
     * 获取订单详情
     * @url /order/:id
     * @param $id
     * @return $this
     * @throws OrderException
     */
    public function getDetail($id){
        (new IDMustBePositiveInt())->goCheck();
        $orderDetail = OrderModel::get($id);
        if(!$orderDetail){
            throw new OrderException();
        }else{
            return $orderDetail->hidden(['prepay_id']);
        }
    }

    /**
     * 根据用户id获取分页订单列表（简要信息）
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getSummaryByUser($page = 1,$size = 15){
        (new PagingParameter())->goCheck();
        $uid = Token::getCurrentUid();
        $pagingOrders = OrderModel::getSummaryByUser($uid,$page,$size);
        if($pagingOrders->isEmpty()){
            return [
                'current_page' => $pagingOrders->currentPage(),
                'data' => []
            ];
        }

        $data = $pagingOrders->hidden(['snap_items','snap_address'])->toArray();
        return [
            'current_page' => $pagingOrders->currentPage(),
            'data' => $data
        ];
    }

    /**
     * 分页获取全部订单列表(简要信息)
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getSummary($page = 1,$size = 20){
        (new PagingParameter())->goCheck();
        $pagingOrders = OrderModel::getSummaryByPage($page,$size);
        if($pagingOrders->isEmpty()){
            return [
                'current_page' => $pagingOrders->currentPage(),
                'data' => []
            ];
        }

        $data = $pagingOrders->hidden(['snap_items','snap_address'])->toArray();
        return [
            'current_page' => $pagingOrders->currentPage(),
            'data' => $data
        ];
    }
    
    /**
     * 订单发货
     * @url /order/delivery?id=1
     * @param $id 订单id
     * @return SuccessMessage
     */
    public function delivery($id){
        (new IDMustBePositiveInt())->goCheck();
        $order = new OrderService();
        $success = $order->delivery($id);
        if($success){
            return new SuccessMessage();
        }
    }
}